<template>
  <div class="forget-container">
    <div style="width: 900px; margin: 0 auto;">
      <div class="head">
        <el-col
          :xs="8"
          :sm="8"
          :md="4"
          :lg="1"
          style="width: 36px; height: 36px; overflow: hidden; margin-right: 10px;"
        >
          <div style="width: 36px; height: 36px;">
            <img :src="site.pc_logo_url" style="width: 100%; height: 100%; margin-left: 9%; border-radius: 50%;" />
          </div>
        </el-col>
        <el-col :span="15">
          <div
            style="color: #666; font-size: 24px; font-weight: 900; line-height: 36px;"
          >
            {{site.pc_title}}
          </div>
        </el-col>
      </div>
      <!--<el-steps :active="active" align-center style="margin-top: 20px">-->
        <!--<el-step title="验证手机号"></el-step>-->
        <!--<el-step title="设置新密码"></el-step>-->
        <!--<el-step title="完成"></el-step>-->
      <!--</el-steps>-->
      <el-row class="content_info">
        <el-col :span="24" class="login_input">
          <el-form
            v-if="active == 1"
            ref="form"
            :model="form"
            :rules="rules"
            class="login-form"
            label-position="top"
            label-width="15%"
          >
            <el-form-item label="请输入右侧图形验证码" prop="captcha" v-if="imgcode">
             <div class="flex flex-a-c">
                <div class="imgwrap">
                  <el-input v-model="form.captcha" placeholder="请输入右侧图形验证码" style="width: 70%;"></el-input>
                  <img class="imgCode" :src="imgcode" @click="getimgdata" />
                </div>
              </div>
            </el-form-item>
            <el-form-item label="请输入绑定的手机号" prop="mobile">
              <el-input
                v-model="form.mobile"
                placeholder="请输入绑定的手机号"
                style="width: 70%;"
              ></el-input>
            </el-form-item>
            <el-form-item label="输入手机短信验证码" prop="code">
              <div style="display: flex;">
                <el-input
                  v-model="form.code"
                  placeholder="输入手机短信验证码"
                  style="width: 70%;"
                ></el-input>
                <el-button
                  v-show="show"
                  type="primary"
                  @click.native.prevent="getCode"
                  class="btn-sty"
                  style="margin-left: 10px;"
                >
                  获取手机验证码
                </el-button>
                <el-button v-show="!show" type="primary" disabled size="small" class="btn-sty">
                  {{ count }}秒后重新获取
                </el-button>
              </div>
            </el-form-item>

            <el-form-item label="新密码" prop="password">
              <el-input
                v-model="form.password"
                :type="passwordType"
                placeholder="请输入密码"
                style="width: 70%;"
              ></el-input>
              <span class="show-pwd" @click="showPwd">
                <img
                  :src="passwordType === 'password' ? require('@/assets/images/no_view_password.png') : require('@/assets/images/view_password.png')"
                  :style="passwordType === 'password' ? 'width: 1.094rem;height: 0.719rem;' : 'width: 1.188rem;height: 0.563rem;'"
                />
              </span>
            </el-form-item>
            <el-form-item label="确认新密码" prop="re_password">
              <el-input
                v-model="form.re_password"
                :type="passwordType"
                placeholder="请再次输入密码"
                style="width: 70%;"
              ></el-input>
            </el-form-item>
            <el-form-item style="margin-left: 25%;">
              <el-button type="primary" @click.prevent="submitForm" class="btn-sty">
                提交修改
              </el-button>
            </el-form-item>
          </el-form>

          <div v-if="active == 2" class="complete">
            <i class="el-icon-success blue"></i><br />
            <span class="text">设置密码成功</span>
            <el-button plain @click="toLogin" class="btn-sty" style="color: white;">点击返回登录页面</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import Vue from 'vue';
import { Button , Checkbox , Row , Col , Message} from "element-ui";
Vue.use(Button).use(Checkbox).use(Row).use(Col);
import http from './js/http.js'
Vue.prototype.$message = Message;

export default {
  name: "Forget",
  components: {},
  data() {
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      site: {},
      active: 1,
      logo:"",
      name:'',
      form: {
        mobile: "",
        code: "",
        password: "",
        re_password: "",
        captcha:""
      },
      rules: {
        mobile: [{ required: true, trigger: "blur", message: "请输入手机号" }],
        code: [{ required: true, trigger: "blur", message: "请输入验证码" }],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
        re_password: [
          { validator: validatePass2, required: true, trigger: "blur" }
        ],
        captcha: [{ required: true, trigger: "blur", message: "请输入图形验证码" }],
      },
      show: true,
      count: "",
      passwordType: "password",
      imgcode:"" //图形验证码
    };
  },
  watch: {},
  created() {
    // this.getLoginSite();
    this.getGlobalData();
    this.getimgdata();
  },
  destroyed() {},
  methods: {
    getGlobalData() {
      http
        .get("plugin.sub-platform.frontend.setting.index")
        .then(response => {
          if (response.result === 1) {
            this.site = response.data;
            // localStorage.setItem("setPublicSetting", JSON.stringify(this.site));
          }
        })
        .catch(({data,result,msg}) => {
          this.$message.error(msg);
        });
    },
    getLoginSite() {
      http
        .get("/admin/login/site", {})
        .then(response => {
          if (response.result === 1) {
            this.logo = response.data.site_logo;
            this.name = response.data.name;
          } else {
            // MessageBox.alert(response.msg);
            this.$message(response.msg);
          }
        })
        .catch(({data,result,msg}) => {
          this.$message.error(msg);
        });
    },
    getCode() {
      if (this.fun.isMoblie(this.form.mobile)) {
        this.$message("请输入正确的手机号码");
        return;
      }
      http
        .post("member.register.sendCodeV2", { mobile: this.form.mobile, captcha: this.form.captcha,reset: "1", sms_type: 2 }, " ")
        .then(response => {
          if (response.result === 1) {
            this.getTimer();
          } else {
            this.form.captcha = "";
            this.getimgdata();
            // MessageBox.alert(response.msg);
            this.$message(response.msg);
          }
        })
        .catch(({data,result,msg}) => {
          this.$message.error(msg);
        });
    },
    next() {
      if (!this.form.code) {
        this.$message("请输入验证码");
        return;
      }
      this.active++;
      // $http
      //   .post(
      //     "/admin/checkCode/",
      //     { mobile: this.form.mobile, code: this.form.code },
      //     " "
      //   )
      //   .then(response => {
      //     if (response.result === 1) {
      //       this.active++;
      //     } else {
      //       MessageBox.alert(response.msg);
      //     }
      //   })
      //   .catch(err => {
      //     console.log(err);
      //   });
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          http
            .post(
              "member.register.changePassword",
              {
                mobile: this.form.mobile,
                code: this.form.code,
                password: this.form.password,
                confirm_password: this.form.re_password,
                captcha: this.form.captcha
              },
              "提交中"
            )
            .then(response => {
              if (response.result === 1) {
                this.$message("设置成功");
                this.$refs["form"].resetFields();
                this.active = this.active + 1;
              } else {
                this.$message(response.msg);
              }
            })
            .catch(err => {
              this.$message(err.msg);
              console.error(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    toLogin() {
      this.$router.push(this.fun.getUrl("subplatformLogin"));
      // this.$router.push({ path: "/login?i="+this.fun.getKeyByI() });
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
    },
    getTimer() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
     //获取图片验证码
    getimgdata() {
      http
        .get("home-page.getCaptcha", {})
        .then(response => {
          if (response.result === 1) {
            this.imgcode = response.data.captcha.img;
          } else {
            console.log(response.msg);
          }
        })
        .catch(({data,result,msg}) => {
          this.$message.error(msg);
        });
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #fff;
$dark_gray: #889aa4;
$light_gray: #eee;

.forget-container {
  // min-height: 100%;
  height: 100vh;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .forget {
    font-size: 14px;
    color: #409eff;
    cursor: pointer;
    position: absolute;
    right: 2rem;
  }

  .head {
    display: flex;
    margin-top: 60px;
    padding-bottom: 15px;
    border-bottom: 1px solid #e5e5e5;
  }

  .content_info {
    margin-top: 40px;
    display: flex;
    align-items: center;

    .login_input {
      box-shadow: 0 0 8px rgba(0, 0, 0, 0.08);
      background: #fff;

      input:focus {
        color: #333;
      }

      .imgwrap {
        position: relative;
        display: flex;
        align-items: center;

        img {
          margin-left: 5px;
        }
      }
    }
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 30px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    text-align: left;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      // color: $light_gray;
      color: #333;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 4rem;
    top: 5px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
}

.complete {
  width: 50%;
  margin: 3rem auto;
  text-align: center;

  .text {
    display: block;
    margin-bottom: 1rem;
  }

  .blue {
    color: #409eff;
    font-size: 70px;
    margin-bottom: 1rem;
  }
}

.btn-sty {
  background-color: #29ba9c !important;
  border-color: #29ba9c !important;
}
</style>
